package com.lucian.ge.controller.shopportal;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lucian.ge.auth.controller.GetAuthController;
import com.lucian.ge.comman.model.ResultMapSyn;
import com.lucian.ge.dao.shop.PayInfoMapper;
import com.lucian.ge.service.shop.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.lucian.ge.comman.model.ResultMap;
import com.lucian.ge.comman.model.ResultMapSyn;


import java.util.*;
import java.util.concurrent.TimeUnit;


/**
 * Created by Lucicol on 2019/7/9.
 */
@Api(value = "OrderController", description = "order-订单接口")

@RestController
@RequestMapping("/order")
public class OrderController {
    private final ResultMap resultMap;
    private final ResultMapSyn resultMapSyn;

    @Autowired
    public OrderController(ResultMap resultMap, ResultMapSyn resultMapSyn) {
        this.resultMap = resultMap;
        this.resultMapSyn = resultMapSyn;
    }

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired(required=false)
    private OrderService orderService;

    @Autowired(required=false)
    private PayInfoMapper payInfoMapper;

    @ApiOperation(value="创建订单", notes="微信小程序")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @PostMapping("/create")
    @ResponseBody
    public ResultMap create(@RequestHeader String token,
                            @RequestParam(value = "userid") Integer userId,
                            @RequestParam(value = "remark") String remark,
                            @RequestParam(value = "orderlist") String orderList,
                            @RequestParam(value = "shippingwx") String shippingWx){
        if (!GetAuthController.getUserAuth(token, userId).equals("普通注册用户验证通过")) {
            return resultMap.success().code(301).message(GetAuthController.getUserAuth(token, userId)).token("").res("").data(null);
        }
//        System.out.println(orderList);
        orderService.create(userId, shippingWx, orderList, remark);

        return resultMap.success().code(200).message("订单创建成功").token("").res("").data(null);
    }

    /**
     * 获得订单列表
     */
    @ApiOperation(value="获得订单列表", notes="")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @GetMapping("/get_orderlist")
    @ResponseBody
    public ResultMap get_orderlist (@RequestHeader String token, @RequestParam(value = "userid") Integer userId, @RequestParam(value = "status", required = false) Integer status, @RequestParam(value = "page", required = false) Integer page) {
        if (!GetAuthController.getUserAuth(token, userId).equals("普通注册用户验证通过")) {
            return resultMap.success().code(301).message(GetAuthController.getUserAuth(token, userId)).token("").res("").data(null);
        }
        List<Map<String, Map<String, String>>>  orderList = orderService.getOrderList(userId, status);
        return resultMap.success().code(200).message("获得订单列表成功").token("").res(orderList).data(null);
    }

    /**
     * 获得订单子表详情
     */
    @ApiOperation(value="获得订单子表详情", notes="")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @GetMapping("/get_orderitemlistdetail")
    @ResponseBody
    public ResultMap get_order_itemlist_detail (@RequestHeader String token, @RequestParam(value = "userid") Integer userId, @RequestParam(value = "orderno") String orderNo) {
        if (!GetAuthController.getUserAuth(token, userId).equals("普通注册用户验证通过")) {
            return resultMap.success().code(301).message(GetAuthController.getUserAuth(token, userId)).token("").res("").data(null);
        }
        List<Map<String, String>>  orderDetailList = orderService.getOrderItemListDetail(userId, orderNo);
        try { Thread.currentThread().sleep(50); }
        catch(Exception e){}
//        System.out.println(orderDetail);
//        System.out.println(orderDetailList);
//        System.out.println(resultMap.hashCode());
//        System.out.println(resultMap.res(orderDetailList).hashCode());
//        System.out.println(resultMap.res(orderDetailList));

        return resultMap.success().code(200).message("获得订单子表详情成功").token("").res(orderDetailList).data(null);
    }

    /**
     * 获得订单详情
     */
    @ApiOperation(value="获得订详情", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @GetMapping("/get_orderdetail")
    @ResponseBody
    public ResultMapSyn get_order_detail (@RequestHeader String token, @RequestParam(value = "userid") Integer userId, @RequestParam(value = "orderno") String orderNo) {
        if (!GetAuthController.getUserAuth(token, userId).equals("普通注册用户验证通过")) {
            return resultMapSyn.success().code(301).message(GetAuthController.getUserAuth(token, userId)).token("").res("").data(null);
        }
        Map<String, String>  orderDetail = orderService.getOrderDetail(userId, orderNo);
//        System.out.println(resultMap.res(orderDetail).hashCode());
//        System.out.println(resultMap.res(orderDetail));
        return resultMapSyn.success().code(200).message("获得订详情成功").token("").res(orderDetail).data(null);
    }


//    @ApiOperation(value="获得订详情", notes="")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
//    })
//    @GetMapping("/get_orderdetail")
//    @ResponseBody
//    public ResultMap get_order_detail (@RequestHeader String token, @RequestParam(value = "userid") Integer userId, @RequestParam(value = "orderno") String orderNo) {
//        if (!GetAuthController.getUserAuth(token, userId).equals("普通注册用户验证通过")) {
//            return resultMap.success().code(301).message(GetAuthController.getUserAuth(token, userId)).token("").res("").data(null);
//        }
//        Map<String, String>  orderDetail = orderService.getOrderDetail(userId, orderNo);
//        System.out.println(resultMap.res(orderDetail).hashCode());
//        System.out.println(resultMap.res(orderDetail));
//        return resultMap.success().code(200).message("获得订详情成功").token("").res(orderDetail).data(null);
//    }

    /**
     * 订单支付
     */
    @ApiOperation(value="订单支付", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @PostMapping("/order_pay")
    @ResponseBody
    public ResultMapSyn order_pay (@RequestHeader String token,
                                   @RequestParam(value = "userid") Integer userId,
                                   @RequestParam(value = "openid") String openId,
//                                   @RequestParam(value = "shareid") String shareId,
                                   @RequestParam(value = "orderno") String orderNo) {
        if (!GetAuthController.getUserAuth(token, userId).equals("普通注册用户验证通过")) {
            return resultMapSyn.success().code(301).message(GetAuthController.getUserAuth(token, userId)).token("").res("").data(null);
        }

        orderService.changeOrderStatus(userId, orderNo, "20");
        payInfoMapper.createWXMiniPay(userId, Long.parseLong(orderNo), openId);
//        分享id
//        if ("undefined".equals(shareId)) {
//            orderService.addShareId(userId, orderNo, "0");
//        } else {
//            orderService.addShareId(userId, orderNo, shareId);
//        }
        return resultMapSyn.success().code(200).message("订单支付成功").token("").res("").data(null);
    }

    /**
     * 确认收货
     */
    @ApiOperation(value="确认收货", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @PostMapping("/confirm_receipt")
    @ResponseBody
    public ResultMapSyn confirm_receipt (@RequestHeader String token,
                                   @RequestParam(value = "userid") Integer userId,
                                   @RequestParam(value = "orderno") String orderNo) {
        if (!GetAuthController.getUserAuth(token, userId).equals("普通注册用户验证通过")) {
            return resultMapSyn.success().code(301).message(GetAuthController.getUserAuth(token, userId)).token("").res("").data(null);
        }
        orderService.changeOrderStatus(userId, orderNo, "50");
        return resultMapSyn.success().code(200).message("确认收货成功").token("").res("").data(null);
    }

    /**
     * 订单取消
     */
    @ApiOperation(value="订单取消", notes="")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
//    })
    @GetMapping("/cancel")
    @ResponseBody
    public void cancel () {
//        redisTemplate.opsForList().leftPush("queue", "1111111111");
//        redisTemplate.opsForList().leftPush("queue", "2222222222");
//        while (true) {
//            String message = redisTemplate.opsForList().rightPop("queue", 1000L, TimeUnit.SECONDS);
//            try { Thread.currentThread().sleep(1000); }
//            catch(Exception e){}
//            System.out.println("接收到了" + message);
//        }
//        return resultMapSyn.success().code(200).message("订单取消成功").token("").res("").data(null);

    }

}
